package cn.itcast.controller;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderSettingVo;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingsService;
import cn.itcast.utils.DateUtils;
import cn.itcast.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(path ="/ordersetting")
public class OrderSettingsController {
    @Reference
    private OrderSettingsService orderSettingsService;

    @PutMapping(path="/updateOrderSettingNumber")
    public Result updateOrderSettingNumber(@RequestBody OrderSetting orderSetting){
        //1.判断是否是第一次设置
        try {
            Boolean isFirst = orderSettingsService.
                    isFirstOrderSetting(orderSetting.getOrderDate());
            if (isFirst){
                //是第一次设置
                orderSettingsService.saveOrderSettingNumber(orderSetting);
                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
            }
            //不是第一次设置
            Boolean flag = orderSettingsService.updateOrderSettingNumber(orderSetting);
            //2.判断已预约是否大于可预约
            if (!flag){
                //已预约大于可预约,更新失败
                return new Result(false, MessageConstant.ORDERSETTING_FAIL);
            }
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
    }

    @GetMapping(path="/getOrderSettingsByMonth")
    public Result getOrderSettingsByMonth(String date){
        try {
            List<OrderSettingVo> orderSettingVoList =
                    orderSettingsService.getOrderSettingsByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettingVoList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }

    }

    @PostMapping(path = "/upload")
    public Result updateOrderSettingNumberByFile(MultipartFile excelFile){
        try {
            //1.读取文件内容,将每行中的值作为一个数组，所有行作为一个集合
            List<String[]> settings = POIUtils.readExcel(excelFile);
            if (null==settings){
                //文件内容为空
                return new Result(false,MessageConstant.UPLOAD_FAIL);
            }
            //2.循环写入orderSetting表
            for (String[] setting : settings) {
                //新建一个对象用于封装数据
                OrderSetting orderSetting = new OrderSetting();
                Date orderDate = DateUtils.parseString2Date(setting[0],"yyyy/MM/dd");
                Integer number = Integer.parseInt(setting[1]);
                orderSetting.setOrderDate(orderDate);
                orderSetting.setNumber(number);
                //判断是否第一次创建,不是就创建,是就修改
                Boolean isFirst = orderSettingsService.isFirstOrderSetting(orderDate);
                if (isFirst){
                    //第一次,新建
                    orderSettingsService.saveOrderSettingNumber(orderSetting);
                }
                //修改
                Boolean flag = orderSettingsService.updateOrderSettingNumber(orderSetting);
                //判断已预约是否大于可预约
                if (!flag){
                    //已预约大于可预约,更新失败
                    return new Result(false,MessageConstant.UPLOAD_FAIL);
                }
            }
            return new Result(true,MessageConstant.UPLOAD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true,MessageConstant.UPLOAD_SUCCESS);
    }

}
